package com.dingding.order.core.pay.biz.pay.impl;

import java.math.BigDecimal;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.facade.account.user.service.IUserAccountService;
import com.dingding.order.code.OrderPayCodeMsg;
import com.dingding.order.core.pay.biz.pay.IPaymentService;
import com.dingding.order.dto.pay.ReqOrderPayDTO;
import com.dingding.order.dto.pay.ReqTaxiOneLinePayDTO;

/**
 * 
 * @ClassName: DingbiPayment 
 * @Description: 叮币扣款
 * @author CHEHAUNBO 
 * @date 2016年7月5日 上午10:32:57
 * @since V2.3.0
 * 
 */
@Component("dingbiPayment")
public class DingbiPaymentImpl implements IPaymentService {

	/**
	 * 用户账户service
	 */
	@Resource(name = "userAccountService")
	private IUserAccountService userAccountService;

	/**
	 * 
	 * 叮币支付（扣款）
	 * 
	 * @Title: payment
	 * @param object 支付客户端请求DTO
	 * @return boolean true 支付成功 false 支付失败
	 * @author CHEHAUNBO
	 * @since V2.3.0
	 * 
	 */
	@Override
	public <T> boolean payment(Object object, ResponseEntity<T> responseEntity) {

		// 扣除金额
		BigDecimal deductMoney = new BigDecimal(0);
		// 支付用户ID
		String userId = null;
		if ((object instanceof ReqTaxiOneLinePayDTO)) {
			deductMoney = new BigDecimal(((ReqTaxiOneLinePayDTO) object).getPayMoney().toString());
			userId = ((ReqTaxiOneLinePayDTO) object).getPassengerId();
		} else if ((object instanceof ReqOrderPayDTO)) {
			deductMoney = new BigDecimal(((ReqOrderPayDTO) object).getPayMoney().toString());
			userId = ((ReqOrderPayDTO) object).getPassengerID();
		}
		// 执行订单支付
		if (!userAccountService.taxiDeductRemainMoney(userId, deductMoney)) { // 支付失败
			responseEntity.setCode(OrderPayCodeMsg.DEDUCT_MONEY_FAIL.getCode());
			responseEntity.setErrorMessage(OrderPayCodeMsg.DEDUCT_MONEY_FAIL.getMessage());
			responseEntity.setPromptMessage(OrderPayCodeMsg.DEDUCT_MONEY_FAIL.getMessage());
			return false;
		}
		return true;

	}

}
